package com.sim.simserial

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.plusAssign
import com.drake.channel.receiveEvent
import com.infinit.selfhelp.common.base.BaseActivity
import com.infinit.selfhelp.common.widget.KeepStateNavigator
import com.print.printusb.UsbDriverManager
import com.sim.serialport.library.seria.SerialManager
import com.sim.serialport.library.seria.ext.bytesToHex
import com.sim.serialport.library.seria.ext.hexStringToByteArray
import com.sim.simserial.databinding.MainActivityBinding
import com.sim.simserial.navigation.MainNavigator
import dagger.hilt.android.AndroidEntryPoint
import java.io.File
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : BaseActivity() {
    private val navController: NavController by lazy {
        findNavController(R.id.nav_host_fragment)
    }
    private lateinit var binding: MainActivityBinding


    @Inject
    lateinit var homeNavigator: MainNavigator

    @Inject
    lateinit var serialManager: SerialManager

    @Inject
    lateinit var usbDriverManager: UsbDriverManager


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = MainActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        usbDriverManager.initUsbDriverByActivity(this)
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
        val navigator = navHostFragment?.childFragmentManager?.let {
            KeepStateNavigator(this, it, R.id.nav_host_fragment)
        }
        navigator?.let {
            navController.navigatorProvider += it
        }
        navController.setGraph(R.navigation.nav_graph)
        homeNavigator.bindNavController(navController)

        receiveEvent<RegisterSimReaderEvent> {
            serialManager.job2 ?: run {
                serialManager.receiveData()
            }
        }
    }

    class RegisterSimReaderEvent()
}
